const { Sdk_Platform } = require("../../frame/constants");

// 因为不允许 index 所以写出来
module.exports = {
    copy(content) {
        return JSON.parse(JSON.stringify(content))
    },
    arrEquip(A, B) {
        if (A.sort().toString() == B.sort().toString()) {
            return true
        } else {
            return false
        }
    },
    isRepeat(arr) {
        let hash = {};
        for (let i in arr) {
            if (hash[arr[i]]) {
                return true;
            }
            hash[arr[i]] = true;
        }
        return false;
    },
    isArrinArrayRepeat(parent, child) {
        // 数组 在不在 数组集合中
        if (parent.length) {
            for (let i = 0; i < parent.length; i++) {
                // 如果有一个不存在 就 return false
                for (let j = 0; j < child.length; j++) {
                    if (parent[i].indexOf(child[j]) < 0) {
                        return false
                    }
                }
            }
            return true
        }
        return false

    },
    createRegion(texture) {
        let skeletonTexture = new sp.SkeletonTexture()
        skeletonTexture.setRealTexture(texture)
        let page = new sp.spine.TextureAtlasPage()
        page.name = texture.name
        page.uWrap = sp.spine.TextureWrap.ClampToEdge
        page.vWrap = sp.spine.TextureWrap.ClampToEdge
        page.texture = skeletonTexture
        page.texture.setWraps(page.uWrap, page.vWrap)
        page.width = texture.width
        page.height = texture.height

        let region = new sp.spine.TextureAtlasRegion()
        region.page = page
        region.width = texture.width
        region.height = texture.height
        region.originalWidth = texture.width
        region.originalHeight = texture.height

        region.rotate = false
        region.u = 0
        region.v = 0
        region.u2 = 1
        region.v2 = 1
        region.texture = skeletonTexture
        return region
    },
    // target 参数为 node
    getActionpause(target) {
        let actionManager = cc.director.getActionManager()
        var element = actionManager._hashTargets[target._id];
        if (element)
            return element.paused
        return false
    },
    // 补零
    prefixInteger(num, n) {
        if (String(num).length > n) {
            return num
        }
        return (Array(n).join(0) + num).slice(-n);
    },
    // 时间转字符
    secTostr(v) {
        let min = parseInt(v / 60)
        let hour = parseInt(min / 60)
        let sec = v % 60
        let str = `${this.prefixInteger(hour, 2)}:${this.prefixInteger(min - hour * 60, 2)}:${this.prefixInteger(sec, 2)}`
        return str
    },
    //只分钟
    sceTostrSec(v) {
        let min = parseInt(v / 60)
        let sec = v % 60
        let str = `${this.prefixInteger(min, 2)}:${this.prefixInteger(sec, 2)}`
        return str
    },
    // 连线所有点的面积
    DotToSize(list) {
        let count = 0
        for (let i = 0; i < list.length; i++) {
            let res = 0
            // if(!i){
            //     res = (list[i].x - list[list.length - 1].x) * (list[i].y + list[list.length - 1].y)
            // }else{
            //     res = (list[i].x - list[i - 1].x) * (list[i].y + list[i - 1].y)
            // he.log(list[i][0] , list[list.length - 1][0],list[i][1] , list[list.length - 1][1])
            // }
            if (!i) {
                res = (list[i][0] - list[list.length - 1][0]) * (list[i][1] + list[list.length - 1][1])
            } else {
                res = (list[i][0] - list[i - 1][0]) * (list[i][1] + list[i - 1][1])
            }
            count += res
        }
        return Math.abs(count / 2)
    },
    refreshItemList(itemList, dataList, newfunc, refreshFunc) {
        for (let i = 0; i < dataList.length; i++) {
            let item = itemList[i]
            if (item === undefined) {
                item = newfunc(i)
                itemList.push(item)
            }
            if (item) {
                item.node.active = true
                refreshFunc(item, i, dataList[i])
            }
        }

        for (let i = dataList.length; i < itemList.length; i++) {
            let item = itemList[i]
            if (item) {
                item.node.active = false
            }
        }
    },
    //返回两个数组中不同的元素
    getArrDifference(arr1, arr2) {
        return arr1.concat(arr2).filter(function (v, i, arr) {
            return arr.indexOf(v) === arr.lastIndexOf(v);
        });
    },
    //对象数组快排
    //arr:数组
    //dir:排序方向，asc升序，desc降序“”
    //key:排序依据的对象属性“”
    sortObj(arr, dir, key) {
        dir = dir || "asc";
        if (arr.length == 0) return [];

        var leftArr = [];
        var rightArr = [];
        var datum = arr[0];

        if (dir === "asc") {
            for (var i = 1; i < arr.length; i++) {
                arr[i][key] < datum[key] ? leftArr.push(arr[i]) : rightArr.push(arr[i]);
            }
        } else {
            for (var i = 1; i < arr.length; i++) {
                arr[i][key] > datum[key] ? leftArr.push(arr[i]) : rightArr.push(arr[i]);
            }
        }
        return this.sortObj(leftArr, dir, key).concat(datum, this.sortObj(rightArr, dir, key));
    },
    // 创建授权按钮
    createAuthorizeBtn(btnNode, fuc) {
        let btnSize = cc.size(btnNode.width, btnNode.height);
        let frameSize = cc.view.getFrameSize();
        let winSize = cc.director.getWinSize();
        let worldPos = btnNode.convertToWorldSpaceAR(cc.Vec2.ZERO)
        // console.log("winSize: ",winSize.toString());
        // console.log("worldPos: ",worldPos.toString());
        // console.log("frameSize: ",frameSize.toString());
        //适配不同机型来创建微信授权按钮
        let left = (worldPos.x - btnSize.width / 2) / winSize.width * frameSize.width
        let top = (winSize.height - worldPos.y - btnSize.height / 2) / winSize.width * frameSize.width
        let width = btnSize.width / winSize.width * frameSize.width;
        let height = btnSize.height / winSize.height * frameSize.height;
        let btnAuthorize = wx.createUserInfoButton({
            type: 'text',
            text: '',
            style: {
                left: left,
                top: top,
                width: width,
                height: height,
                lineHeight: 0,
                backgroundColor: '',
                color: '#ffffff',
                textAlign: 'center',
                fontSize: 16,
                borderRadius: 4
            }
        })
        btnAuthorize.onTap((res) => {
            let retrunRes = res
            let auth = he.local.getJson('userinfo', 'user')
            if (auth) {
                if (fuc) {
                    fuc(retrunRes)
                }
                return
            }
            if (gg && gg.uiManager && gg.uiManager.lastScene != 'Main') {
                return
            }

            if (res.userInfo) {
                // let uid = he.local.get('uid', 'user')
                wx.cloud.callFunction({
                    name: 'addUserinfo',
                    success: res => {
                        if (res.result) {
                            he.local.setJson('userinfo', res.result, 'user')
                            if (fuc) {
                                fuc(retrunRes)
                            }
                            if (window.gg) {
                                gg.eventManager.emit('topUser', Control_Listen.UPDATE_USERINFO)
                            }
                            wx.showToast({ title: "授权成功" });
                        } else {
                            if (fuc) {
                                fuc(res)
                            }
                        }
                    },
                    fail: err => {
                        console.log(err)
                        wx.showToast({ icon: 'none', title: "授权失败" });
                    },
                    data: res.userInfo
                })
                // wx.showToast({title:"授权成功"});
            } else {
                wx.showToast({ icon: 'none', title: "授权失败" });
            }
        })
        return btnAuthorize
    },
    createGameClubBtn(btnNode) {
        let btnSize = cc.size(btnNode.width, btnNode.height);
        let frameSize = cc.view.getFrameSize();
        let winSize = cc.director.getWinSize();
        let worldPos = btnNode.convertToWorldSpaceAR(cc.Vec2.ZERO)
        //适配不同机型来创建微信授权按钮
        let left = (worldPos.x - btnSize.width / 2) / winSize.width * frameSize.width
        let top = (winSize.height - worldPos.y - btnSize.height / 2) / winSize.width * frameSize.width
        let width = btnSize.width / winSize.width * frameSize.width;
        let height = btnSize.height / winSize.height * frameSize.height;
        let btnClub = wx.createGameClubButton({
            type: 'text',
            text: '',
            style: {
                left: left,
                top: top,
                width: width,
                height: height,
                lineHeight: 0,
                backgroundColor: '',
                color: '#ffffff',
                textAlign: 'center',
                fontSize: 16,
                borderRadius: 4
            }
        })
        return btnClub
    },
    getLongScreen() {
        if (zoom_scale < 0.85 && cc.view.getFrameSize().height > 750) {
            return true
        }
        return false
    },
    getRandomIndexList(num) {
        let list = []
        while (list.length < num) {
            let res = parseInt(Math.random() * num)
            if (list.indexOf(res) < 0) {
                list.push(res)
                if (list.length == num) {
                    return list
                }
            }
        }
    },
    checkCrossDay(pre) {
        if (!pre) {
            he.error('没有传参')
            return false
        }
        pre += 86400000
        let pre_date = new Date(pre)
        let pre_day_str = pre_date.getFullYear() * 10000 + pre_date.getMonth() * 100 + pre_date.getDate()
        let dida = he.local.get('local_dida')
        if (!dida) {
            dida = 0
        }
        dida = 0
        let now = new Date().getTime() + dida
        let now_date = new Date(now)
        let now_day_str = now_date.getFullYear() * 10000 + now_date.getMonth() * 100 + now_date.getDate()
        if (parseInt(now_day_str) >= parseInt(pre_day_str)) {
            return true
        } else {
            return false
        }
    },
    // 震动
    shake(long) {
        switch (common.sdk_env) {
            case Sdk_Platform.WECHAT:
                if (long) {
                    wx.vibrateLong()
                } else {
                    wx.vibrateShort()
                }
                break
        }
    },
    // 虚假
    adFakeTip() {
        let ver_info = he.local.getJson('ver_info')
        if (ver_info) {
            if (ver_info.fake_video) {
                gg.scheduleOnce(() => {
                    gg.uiManager.addTip("点击下方绿色按钮可获得 300颗 钻石奖励")
                }, 6);
            }
        }

    },
}